<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .goods-list {
            width: 1226px;
            margin: 0 auto;
            display: flex;
            flex-wrap: wrap;
            font-size: 12px;
            margin-top: 20px;
        }

        .goods-list li {
            width: 20%;
            box-sizing: border-box;
            padding: 5px 10px;
        }

        .goods-list li:hover {
            box-shadow: 0px 0px 1px 1px rgba(0, 0, 0, 0.4);
        }

        .goods-list li img {
            width: 80%;
            display: block;
            margin: 0px auto;
            margin-top: 10px;
        }

        .price {
            line-height: 32px;
            color: #e43981;
        }

        .desc {
            line-height: 24px;
            /* 老版本弹性盒 */
            display: -webkit-box;
            /* 超出两行隐藏 */
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>
</head>

<body>
    <ul class="goods-list">
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/42776/10/17345/131277/626bc1e3Eb2b0278a/1dc51316f3dc1898.jpg.avif"
                    alt="">
                <div class="price">￥4499.00</div>
                <div class="desc">笔记本电脑MateBook D 14 2022款 14英寸 11代酷睿 i5 16G+512G 锐炬显卡 轻薄本/护眼全面屏 银</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/42776/10/17345/131277/626bc1e3Eb2b0278a/1dc51316f3dc1898.jpg.avif"
                    alt="">
                <div class="price">￥4499.00</div>
                <div class="desc">笔记本电脑MateBook D 14 2022款 14英寸 11代酷睿 i5 16G+512G 锐炬显卡 轻薄本/护眼全面屏 银</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/42776/10/17345/131277/626bc1e3Eb2b0278a/1dc51316f3dc1898.jpg.avif"
                    alt="">
                <div class="price">￥4499.00</div>
                <div class="desc">笔记本电脑MateBook D 14 2022款 14英寸 11代酷睿 i5 16G+512G 锐炬显卡 轻薄本/护眼全面屏 银</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/42776/10/17345/131277/626bc1e3Eb2b0278a/1dc51316f3dc1898.jpg.avif"
                    alt="">
                <div class="price">￥4499.00</div>
                <div class="desc">笔记本电脑MateBook D 14 2022款 14英寸 11代酷睿 i5 16G+512G 锐炬显卡 轻薄本/护眼全面屏 银</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/42776/10/17345/131277/626bc1e3Eb2b0278a/1dc51316f3dc1898.jpg.avif"
                    alt="">
                <div class="price">￥4499.00</div>
                <div class="desc">笔记本电脑MateBook D 14 2022款 14英寸 11代酷睿 i5 16G+512G 锐炬显卡 轻薄本/护眼全面屏 银</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/42776/10/17345/131277/626bc1e3Eb2b0278a/1dc51316f3dc1898.jpg.avif"
                    alt="">
                <div class="price">￥4499.00</div>
                <div class="desc">笔记本电脑MateBook D 14 2022款 14英寸 11代酷睿 i5 16G+512G 锐炬显卡 轻薄本/护眼全面屏 银</div>
            </a>
        </li>
        <li>
            <a href="">
                <img src="https://img12.360buyimg.com/n7/jfs/t1/42776/10/17345/131277/626bc1e3Eb2b0278a/1dc51316f3dc1898.jpg.avif"
                    alt="">
                <div class="price">￥4499.00</div>
                <div class="desc">笔记本电脑MateBook D 14 2022款 14英寸 11代酷睿 i5 16G+512G 锐炬显卡 轻薄本/护眼全面屏 银</div>
            </a>
        </li>
    </ul>
</body>
<script>
    // 动态生成之前 先写 静态页面

    // [{ goodsId, goodsName, goodsPrice, goodsImg }, { goodsId, goodsName, goodsPrice, goodsImg }, { goodsId, goodsName, goodsPrice, goodsImg }]

    var goodsList = document.getElementsByClassName("goods-list")[0];


    var xhr = new XMLHttpRequest();

    xhr.open("get", "../data/list.json", true);

    xhr.send();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            // 数据请求成功 
            var result = xhr.responseText;
            var list = JSON.parse(result);
            console.log(list);

            var html = "";
            list.forEach((item) => {
                var { goodsId, goodsName, goodsMsg, goodsPrice, goodsImg } = item;
                console.log(item);
                html += `<li>
                    <a href="">
                        <img src="${goodsImg}"
                            alt="">
                        <div class="price">￥${goodsPrice.toFixed(2)}</div>
                        <div class="desc">${goodsMsg}</div>
                    </a>
                </li>`
            })
            goodsList.innerHTML = html;


        }
    }



</script>

</html>